<?php

namespace App\Http\Controllers\Stat;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Exception;
use Udata;
use Common;

class UdataController extends Controller
{
    /**
    * 
    *
    * @return array
    */
    public function index(Request $request)
    {
        return view( '/lost/toolslost' );
    }

    /**
    * 渲染查询道具金额页
    *
    * @return array
    */
    public function checkBidPage(Request $request)
    {
        return view( '/lost/checkbidpage' );
    }

    /**
    * 渲染道具缺失页面
    *
    * @return json
    */
    public function toolsLost(Request $request)
    {
        try {
            $tvmid = isset($request['tvmid']) ? $request['tvmid'] : '';
            $stime = isset($request['stime']) ? $request['stime'] : '';
            $etime = isset($request['etime']) ? $request['etime'] : '';

            if ($tvmid == '' || $stime == '' || $etime == '') throw new Exception("参数不能为空", 400);
            $stime = strtotime($stime);
            $etime = strtotime($etime);

            $res = Udata::toolsLost($tvmid, $stime, $etime);

            return response()->json(array('code'=>'0', "msg"=>$res))->setCallback();
        } catch (Exception $e) {
            $code = $e->getCode();
            $msg = $e->getMessage();

            if ( !$code ) $code = 500;
            if ( !$msg ) $msg = '服务器内部错误';
            return response()->json(array('code'=>$code, "msg"=>$msg))->setCallback();
        }
    }

    /**
    * 查询道具金额和冻结金额
    *
    * @return json
    */
    public function checkBid(Request $request)
    {
        try {
            $tvmid = isset($request['tvmid']) ? $request['tvmid'] : '';

            if ($tvmid == '') throw new Exception("参数不能为空", 400);

            $res = Udata::checkBid($tvmid);

            if ($res['extra'] == $res['total']) {
                return response()->json(array('code' => '0', 'msg' => '无差别', 'data'=>$res))->setCallback();
            }
            return response()->json(array('code' => '0', 'msg' => '实际减去冻结 : '.($res['total'] - $res['extra']), 'data'=>$res))->setCallback();
        } catch (Exception $e) {
            $code = $e->getCode();
            $msg = $e->getMessage();

            if ( !$code ) $code = 500;
            if ( !$msg ) $msg = '服务器内部错误';
            return response()->json(array('code'=>$code, "msg"=>$msg))->setCallback();
        }
    }

    /**
    * 修改道具数量
    *
    * @return json
    */
    public function editCard(Request $request)
    {
        try {
            $tvmid = isset($request['tvmid']) ? $request['tvmid'] : '';
            $type = isset($request['type']) ? $request['type'] : '';
            $value = isset($request['value']) ? $request['value'] : '';

            if ($tvmid == '' || $type == '' || $value == '') throw new Exception("参数不能为空", 400);
            $check = array(1,2,4,6);
            if (!in_array($type, $check)) throw new Exception("type错误", 400);

            $res = Udata::editCard($tvmid, $type, $value);

            return response()->json(array('code' => '0', 'msg' => $res))->setCallback();
        } catch (Exception $e) {
            $code = $e->getCode();
            $msg = $e->getMessage();

            if ( !$code ) $code = 500;
            if ( !$msg ) $msg = '服务器内部错误';
            return response()->json(array('code'=>$code, "msg"=>$msg))->setCallback();
        }
    }
}
